<?xml version="1.0"?>
<rss version="2.0">
  <channel><title>Java Bridge</title><link>http://bill.welliver.org//space/pike/Java Bridge</link><description>This is a very slightly prettied up version of the presentation I gave at the 2003 Pike Conference in Paderborn. It's basically an overview of what's going on in the Java module. Contact me if you're interested in having a copy of the original text document (it might be easier to read in certain situations).&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;JNI Specification&lt;/b&gt;&lt;p class="paragraph"/&gt;
Defines methods for embedding a Java VM within an application.&#13;
Access to Java objects from the application embedding the Java VM.&#13;
Provides access to functions within the application to the embedded Java VM (native methods)&lt;p class="paragraph"/&gt;
&lt;span class="nobr"&gt;&lt;img height="9" width="8" src="/static/images/Icon-Extlink.png" alt="&amp;#91;external]"/&gt;&lt;a href="http://java.sun.com/products/jdk/1.2/docs/guide/jni/"&gt;http://java.sun.com/products/jdk/1.2/docs/guide/jni/&lt;/a&gt;&lt;/span&gt;&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;The Pike Implementation&lt;/b&gt;&lt;p class="paragraph"/&gt;
Located in the Java module.&#13;
Supported on systems containing a Java v1.2+ installation.&#13;
Known to work with both the Sun JDK and IBM JDK&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Using the Java bridge.&lt;/b&gt;&lt;p class="paragraph"/&gt;
&lt;ul class="minus"&gt;
&lt;li&gt;The Hard Way&lt;/li&gt;
&lt;/ul&gt;  Java.machine&#13;
     starts up a Java VM and leaves the rest to you&lt;p class="paragraph"/&gt;
     Pros: Less overhead (speed and memory-wise), more direct control of your &#13;
       interaction with the Java VM&#13;
     Cons: Tedious, time-consuming to set up, not very Pikeish&lt;p class="paragraph"/&gt;
&lt;ul class="minus"&gt;
&lt;li&gt;The Easy Way&lt;/li&gt;
&lt;/ul&gt;  Java.pkg&#13;
     will load a java package and connect all of the functions and fields so &#13;
     that they (nominally) act like other pike objects.&lt;p class="paragraph"/&gt;
     Pros: much easier to access Java functionality&#13;
     Cons: slower, due to object setup and wrappings, less control over how &#13;
       things are set up and connected, doesn't completely hide you from &#13;
       Javaness&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java VM&lt;/b&gt;&lt;p class="paragraph"/&gt;
create(string|void arg)&#13;
  In theory, you should be able to start a vm with arg as the classpath. &#13;
  In reality, this doesn't work so well.&lt;p class="paragraph"/&gt;
int get_version()&#13;
  Returns the version code of the JVM running.&lt;p class="paragraph"/&gt;
object find_class(string c)&#13;
  Finds the class c, using "/" instead of "." as the class separator.&#13;
  Returns the class or zero if the class was not found, in addition to &#13;
  throwing an exception.&lt;p class="paragraph"/&gt;
object define_class(object cl, string bc)&#13;
  prepare a java class object from a string of java bytecode&#13;
  cl is a class loader object&#13;
  Note: doesn't seem to work properly.&lt;p class="paragraph"/&gt;
int exception_check()&#13;
  checks to see if an exception is being thrown.&lt;p class="paragraph"/&gt;
object exception_occurred()&#13;
  returns the exception object, if one has been thrown.&lt;p class="paragraph"/&gt;
void exception_describe()&#13;
  prints an exception description and backtrace to stderr&lt;p class="paragraph"/&gt;
void exception_clear()&#13;
  clears any exceptions&lt;p class="paragraph"/&gt;
void  fatal(string arg)&#13;
  Raises a fatal error; the VM quits, taking Pike with it.&lt;p class="paragraph"/&gt;
object new_boolean_array(int)&lt;p class="paragraph"/&gt;
object new_byte_array(int)&#13;
object new_char_array(int)&#13;
object new_short_array(int)&#13;
object new_int_array(int)&#13;
object new_long_array(int)&#13;
object new_float_array(int)&#13;
object new_double_array(int)&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Object:&lt;/b&gt;&lt;p class="paragraph"/&gt;
mixed cast(string)&#13;
  casts the object to another datatype. Currently, only casting to string is &#13;
  supported.&lt;p class="paragraph"/&gt;
int `==(mixed)&#13;
  are two objects the same?&lt;p class="paragraph"/&gt;
int _ _hash()&#13;
  calls the hashCode method of the object&lt;p class="paragraph"/&gt;
int is_instance_of(object arg)&#13;
  determines whether the object is an instance of arg&lt;p class="paragraph"/&gt;
object monitor_enter()&#13;
  returns a Java.monitor object&lt;p class="paragraph"/&gt;
object get_object_class()&#13;
  returns the class of an object&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Class:&lt;/b&gt;&#13;
inherits Java Object&lt;p class="paragraph"/&gt;
object register_natives(array(array(string|function)) arg)&#13;
  registers one or more native methods for a class. the class must have the &#13;
  functions defined as "native"&lt;p class="paragraph"/&gt;
  arg&amp;#91;n]&amp;#91;0] is a string containing the method name to define&#13;
  arg&amp;#91;n]&amp;#91;1] is a string describing the method signature to define for the given &#13;
  method&#13;
  arg&amp;#91;n]&amp;#91;2] is a function to be called when the method is called from Java&lt;p class="paragraph"/&gt;
object|0 super_class()&#13;
  Returns the superclass of the object, zero if the object is a &#13;
  java.lang.Object.&lt;p class="paragraph"/&gt;
int is_assignable_from(object c)&#13;
 Returns TRUE if:&#13;
  The argument refers to the same Java class as the object itself.&#13;
  The argument class is a subclass of the object.&#13;
  The argument has the object as one of its interfaces.&lt;p class="paragraph"/&gt;
void throw_new(string arg)&#13;
 Throws an exception or error (if the object is a child one of these classes), &#13;
 with the optional description of arg&#13;
 Produces an exception that may be caught or checked using one of the exception &#13;
 checks.&lt;p class="paragraph"/&gt;
object alloc()&#13;
  allocates a new Java object without invoking its constructors. returns the &#13;
  new object.&lt;p class="paragraph"/&gt;
object new_array(int, object|void)&lt;p class="paragraph"/&gt;
&#13;
object get_method(string name, string sig)&#13;
  gets method name with signature sig from the class. returns the method or &#13;
  zero if the name and signature do not match any in the class.&lt;p class="paragraph"/&gt;
object get_static_method(string, string)&#13;
  gets a static method name with signature sig from the class. returns the &#13;
  method or zero if the name and signature do not match any in the class.&lt;p class="paragraph"/&gt;
object get_field(string name, string sig)&#13;
  gets a field name with a type of sig from the class. returns the&#13;
  field object or zero if the name and signature do not match any in the class.&lt;p class="paragraph"/&gt;
object get_static_field(string, string)&#13;
  gets a static field name with a type of sig from the class. returns the&#13;
  field object or zero if the name and signature do not match any in the class.&lt;p class="paragraph"/&gt;
&#13;
&lt;b class="bold"&gt;Java Throwable&lt;/b&gt;&#13;
inherits Java Object&lt;p class="paragraph"/&gt;
void throw()&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Array&lt;/b&gt;&#13;
inherits Java Object&#13;
_sizeof()&#13;
`&amp;#91;]&#13;
`&amp;#91;]=&#13;
_indices&#13;
_values&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Static Method&lt;/b&gt;&lt;p class="paragraph"/&gt;
mixed `(mixed&amp;#8230; args)&#13;
  calls the method with arguments args.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Method&lt;/b&gt;&lt;p class="paragraph"/&gt;
mixed `(object obj, mixed&amp;#8230; args)&#13;
  calls the method as defined in object obj, with arguments args.&lt;p class="paragraph"/&gt;
mixed call_nonvirtual(object obj, mixed &amp;#8230; args)&#13;
  calls the method as defined in the class the method is derived from, with &#13;
  arguments args.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Field &amp; Static Fields&lt;/b&gt;&lt;p class="paragraph"/&gt;
mixed set(mixed val)&#13;
  sets the value of a field to val.&lt;p class="paragraph"/&gt;
mixed get()&#13;
  returns the value of a field&lt;p class="paragraph"/&gt;
Objects used internally by the module:&lt;p class="paragraph"/&gt;
  Java Monitor&#13;
  Java Natives&#13;
  Java Attachment&lt;p class="paragraph"/&gt;
  &#13;
&lt;b class="bold"&gt;The Java Package interface&lt;/b&gt;&lt;p class="paragraph"/&gt;
The Java.package module provides convenient access to an entire Java Class &#13;
package, performing a number of useful tricks along the way.&lt;p class="paragraph"/&gt;
The module will look for a package, load the class, set up access to all of &#13;
its fields and methods, making it look very much like a Pike object.&lt;p class="paragraph"/&gt;
Usage: &#13;
&lt;div class="code"&gt;&lt;pre&gt;&lt;pre&gt;&#13;
        &amp;gt; &lt;b&gt;&lt;font color=darkgreen&gt;object &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;i=Java.pkg&amp;amp;#91&lt;/font&gt;&lt;/b&gt;;&lt;i&gt;&lt;font color=darkred&gt;"java/lang/Integer"&lt;/font&gt;&lt;/i&gt;];&#13;
        &amp;gt; i(52);&#13;
        &amp;gt; indices(i);&#13;
        Result: ({ &lt;font color=red&gt;/* 14 elements */&lt;/font&gt;&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"wait"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"getClass"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"doubleValue"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"longValue"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"toString"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"equals"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"compareTo"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"hashCode"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"floatValue"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"shortValue"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"notifyAll"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"notify"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"intValue"&lt;/font&gt;&lt;/i&gt;,&#13;
                &lt;i&gt;&lt;font color=darkred&gt;"byteValue"&lt;/font&gt;&lt;/i&gt;&#13;
            })&#13;
          &amp;gt; (&lt;b&gt;&lt;font color=darkgreen&gt;string&lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;&lt;/font&gt;&lt;/b&gt;)i-&amp;gt;toString();&#13;
          Result: &lt;i&gt;&lt;font color=darkred&gt;"52"&lt;/font&gt;&lt;/i&gt;&#13;
&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p class="paragraph"/&gt;
          &#13;
What the Java.package module does not do:&lt;p class="paragraph"/&gt;
&lt;ul class="minus"&gt;
&lt;li&gt;convert Pike datatypes into Java datatype objects&lt;/li&gt;
&lt;li&gt;convert Java datatype objects into Pike datatypes&lt;/li&gt;
&lt;li&gt;attempt to guess what function signature you wish to use if it's at all ambiguous.&lt;/li&gt;
&lt;li&gt;completely mask the Java bridge and its Javaness from the Pike user, though it comes quite a bit of the way.&lt;p class="paragraph"/&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;b class="bold"&gt;Using Java.package&lt;/b&gt;&#13;
  The package interface to Java is straightforward in use. The Java module&#13;
  creates an instance of the package interface called "pkg" on startup.&lt;p class="paragraph"/&gt;
  The package interface exposes its functionality by overloading the indexing &#13;
  operator.&lt;p class="paragraph"/&gt;
  To access a java class, simply ask Java.pkg for it:&lt;p class="paragraph"/&gt;
  &gt; object myclass = Java.pkg&amp;#91;"org/welliver/conference/myclass"];&#13;
  Result: Java.jclass()&lt;p class="paragraph"/&gt;
  An error will be thrown if the requested class cannot be found.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java.jclass&lt;/b&gt;&#13;
  represents a Java Class in Pike.&lt;p class="paragraph"/&gt;
  `(mixed &amp;#8230; args) calls the constructor that most closely matches args. will&#13;
     not attempt to choose if more than one method signature matches.&#13;
  `&amp;#91;n] returns the value of a static field n or a static method object n&#13;
  `-&gt;n returns the value if `&amp;#91;n] unless n&amp;#91;0..0]=="_", in which case it returns:&lt;p class="paragraph"/&gt;
    _fields:  returns a mapping of the field objects in the class&#13;
    _static_fields: returns a mapping of the static field objects in the class&#13;
    _methods: returns a mapping of the method objects in the class&#13;
    _static_methods: returns a mapping of the method objects in the class&#13;
    _wrap_result: returns the wrap result function, used to turn a Java.object &#13;
      into a Java.jobject&#13;
    _method: returns the method selector function&#13;
    _constructor: returns the constructor selector function&#13;
    _alloc: returns the alloc function of the base Java.object&#13;
    _register_natives: returns the register_natives function of the base &#13;
      Java.object&lt;p class="paragraph"/&gt;
    Java.jmethod _method(string n, string sig) &#13;
      returns the java method that matches name n and type signature sig.&#13;
    Java.jconstructor _constructor(string sig)&#13;
       returns the java constructor for this class that matches type signature &#13;
       sig.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java.jconstructor&lt;/b&gt;&#13;
  represents a Java constructor in Pike.&lt;p class="paragraph"/&gt;
  Java.jobject `(mixed &amp;#8230; args)&#13;
    calls the constructor of the class with arguments args.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java.jmethod&lt;/b&gt;&#13;
  represents a Java method in Pike.&lt;p class="paragraph"/&gt;
  Java.jobject `(mixed &amp;#8230; args)&#13;
    calls the function with arguments args.&lt;p class="paragraph"/&gt;
  Java.method for_protos(string sig)&#13;
     returns the Java method matching the type signature sig. Used internally.&lt;p class="paragraph"/&gt;
  Java.jmethod for_object(object obj)&#13;
      returns a jmethod object for the object obj. Used internally.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java.jobject&lt;/b&gt;&#13;
  represents a Java object in Pike.&lt;p class="paragraph"/&gt;
  cast(mixed type)&#13;
     calls the cast function for the underlying Java object. Note that this&#13;
     only works with the argument of "string".&lt;p class="paragraph"/&gt;
&#13;
  `&amp;#91;n]&#13;
    returns the field or method n from the class.&#13;
  `-&gt;n&#13;
    returns the value of `&amp;#91;n] unless n&amp;#91;0..0]=="_", in which case it returns:&lt;p class="paragraph"/&gt;
    _method: returns the method selector function.&#13;
    _obj: returns the low level Java.object object.&#13;
    _monitor_enter: returns the Java.monitor object associated with this class.&lt;p class="paragraph"/&gt;
  _values()&#13;
    returns the values of the object, matching its indices.&#13;
  _indices()&#13;
    returns the indices of the object, ie field and method names.&lt;p class="paragraph"/&gt;
  Java.jmethod _method(string n, string sig) &#13;
    returns the java method that matches name n and type signature sig.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java.jarray&lt;/b&gt;&#13;
   represents a Java array in Pike.&lt;p class="paragraph"/&gt;
   cast(mixed &amp;#8230; arg)&lt;p class="paragraph"/&gt;
   `&amp;#91;n] &#13;
     returns the nth element of the array:&lt;p class="paragraph"/&gt;
     length: returns the number of elements in the array&#13;
     _monitor_enter: returns the monitor object associated with this instance&#13;
     _obj: returns the low level object for this array&lt;p class="paragraph"/&gt;
   `&amp;#91;n]=x&#13;
     attempts to set the value of the nth element to x.&lt;p class="paragraph"/&gt;
   `-&gt;n&#13;
     returns the following:&lt;p class="paragraph"/&gt;
     length: returns the number of elements in the array&#13;
     _monitor_enter: returns the monitor object associated with this instance&#13;
     _obj: returns the low level object for this array&lt;p class="paragraph"/&gt;
   _sizeof()&#13;
   _indices()&#13;
   _values()&lt;p class="paragraph"/&gt;
     overloads to make an array object seem more Pikeish.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Java Convenience Functions&lt;/b&gt;&lt;p class="paragraph"/&gt;
The following functions will convert a Pike datatype to a relatively common &#13;
Java equivalent. Their behavior is general, and any value may be provided to &#13;
them, as long as they are the specified type. Any exceptions are noted below.&lt;p class="paragraph"/&gt;
Java.JInteger(int i)&#13;
Java.JString(string s)&#13;
Java.JFloat(float f)&#13;
Java.JBoolean(int b)&#13;
  any value other than 0 is true, 0 is false.&#13;
Java.JArray(array(mixed) a)&#13;
  can encode an array of any type, however all of the elements must be the &#13;
  same type. that is, you may not mix strings and floats as values in the array&#13;
Java.JHashMap(mapping m)&#13;
Java.JVector(multiset x)&#13;
</description><generator>Fins 0.9.7</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs></channel>
</rss>
